<template>
	<view class="app-container">

		<view class="top">

			<view class="title">
				修改密码
			</view>
			<view class="desc">
				请输入密码，长度8-15
			</view>
		</view>
		<view class="app-container__item">
			<!--注册-->
			<view class="content">
				<view class="input-border">
					<view class="label">手机号</view>
					<view class="line">

					</view>
					<input v-model="formData.phone" type="number" maxlength="11" placeholder="请输入手机号"
						class="input width-75" />
				</view>
				<view class="input-border">
					<view class="label">验证码</view>
					<view class="line">

					</view>
					<view class="width-75 sms">
						<input v-model="formData.code" type="number" class="input " placeholder="请填写验证码"
							:maxlength="6" />
						<!-- <text class="getCode" slot="right" @click="getCode">获取验证码</text> -->
						<button @click="sendCode('FORGOT_PASSWORD', formData.phone)" :disabled="isSend" size="mini"
							class="getCode">
							{{ buttonText }}
						</button>
					</view>

				</view>
				<view class="input-border">
					<view class="label">密码</view>
					<view class="line">

					</view>
					<input v-model="password" type="password" placeholder="请输入新密码" class="input width-75"
						@blur="validatePassword(password, confirmPassword)" />
				</view>
				<view class="input-border">
					<view class="label">密码</view>
					<view class="line">

					</view>
					<input v-model="confirmPassword" type="password" placeholder="请再次输入新密码" class="input width-75"
						@blur="validatePassword(confirmPassword,password)" />
				</view>
			</view>
		</view>
		<view>
			<button class="btnBox" @click="submitBtn()">确定</button>
		</view>
	</view>
</template>

<script setup>
	import {
		onLoad,
		onShow
	} from "@dcloudio/uni-app";
	import {
		ref,
		reactive
	} from "vue";
	import {
		showToast
	} from "@/utils/toast";
	import check from "@/utils/check";
	import useVerifyCode from "@/hook/useVerifyCode";
	import {
		modifyPassword,
		retrievePassword
	} from "@/api/mine";
	import usePassword from "@/hook/usePassword";
	import {
		RSAencrypt
	} from "@/utils/methods";
	onLoad((val) => {
		console.log("val", val)
		status.value = val.status
	});

	onShow(() => {});
	// 注册发送验证码hook
	const [{
		buttonText,
		isSend
	}, {
		sendCode
	}] = useVerifyCode("发送验证码");
	// 注册密码校验hook
	const [validatePassword, validateSinglePassword, validateTwoPassword] =
	usePassword();
	let selectLogin = (val) => {
		console.log("val", val);
		currentType.value = val;
	};
	// 区分是修改密码还是忘记密码
	let status = ref('null')
	// 登录方式切换
	let loginList = ref([{
			label: "账号登录",
			value: 0,
		},
		{
			label: "验证码登录",
			value: 1,
		},
	]);
	let currentType = ref(0);
	// 登录数据
	let formData = ref({
		password: null,
		phone: null,
		code: null,
		confirmPassword: null,
		oemUserNo: 'BU100002'
	});
	let password = ref(null);
	let confirmPassword = ref(null);
	// 必填项校验规则
	const validateRules = [{
			key: "phone",
			message: "请输入账号"
		},
		{
			key: "code",
			message: "请输入验证码"
		},
		{
			key: "password",
			message: "请输入新密码"
		},
		{
			key: "confirmPassword",
			message: "请输入确认密码"
		},
	];
	const checkData = () => {
		formData.value.password = password;
		formData.value.confirmPassword = confirmPassword;
		validateRules.forEach((o) => {
			if (!formData.value[o.key]) {
				showToast(o.message);
				throw Error(o.message);
			}
		});

		if (formData.value.phone.length != 11) {
			showToast("请输入11位手机号码");
			return false;
		}

		return (
			validateSinglePassword(formData.value.password) &&
			validateSinglePassword(formData.value.confirmPassword) &&
			validateTwoPassword(formData.value.password, formData.value.confirmPassword)
		);
	}
	// 确认
	let submitBtn = () => {
		console.log("status.value", status.value)

		if (checkData()) {
			const {
				phone,
				code,
				password,
				confirmPassword
			} = formData.value;

			let params = {
				phone: phone,
				code: code,
				password: password,
				confirmPassword: confirmPassword,
				oemUserNo: 'BU100002'
			}
			params.password = RSAencrypt(params.password);
			params.confirmPassword = RSAencrypt(params.confirmPassword);
			if (status.value == 'retrieve') {
				retrievePassword(params)
					.then((res) => {
						showToast("密码修改成功");
						setTimeout(() => {
							uni.navigateBack();
						}, 1000);
					})
					.catch((res) => {
						console.log("res=" + JSON.stringify(res))
						showToast(res.data.message);
						// submitLoading.value = false;
					});
			} else {
				retrievePassword(params)
					.then((res) => {
						showToast("密码修改成功");
						setTimeout(() => {
							// uni.navigateBack();
							uni.reLaunch({
								url: "/pages/login/index"
							});
						}, 1000);
					})
					.catch((res) => {
						showToast(res.data.message);
						// submitLoading.value = false;
					});
			}

		}

	};
	// 获取短信验证码
	let getCode = () => {
		if (!formData.phone) {
			showToast("请输入手机号", "none");
			return;
		}
		let checkStatus = check.isPhone(formData.phone);
		if (!checkStatus) {
			showToast("请输入正确手机号", "none");
		}
	};
</script>
<style lang="scss" scoped>
	@import "./index.scss";
</style>